package com.itheima.string;

import java.util.Random;
import java.util.Scanner;

public class StringDemo1 {
    public static void main(String[] args) {
        Scanner rn = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请您输入登录的用户名：");
            String name = rn.next();
            System.out.println("请您输入登录的密码：");
            String password = rn.next();
            String randomnumber = randomNumber(4);
            System.out.println("验证码【"+randomnumber+"】：");
            String random = rn.next();
            if (login(name, password)&&randomnumber.equalsIgnoreCase(random)) {
                System.out.println("登录成功~");
                break;
            } else {
                System.out.println("对不起，您输入的用户名、密码或验证码有误~");
            }
        }
    }

    public static boolean login(String name, String password) {
        String names = "itheima";
        String passwords = "123456";
        return (names.equals(name) && passwords.equals(password));
    }

    public static String randomNumber(int n) {
        Random ra = new Random();
        String text = "";
        for (int i = 0; i < n; i++) {
            int type = ra.nextInt(3);
            switch (type) {
                case 0:
                    text += ra.nextInt(10);
                    break;
                case 1:
                    text += (char)(ra.nextInt(26) + 65);
                    break;
                case 2:
                    text += (char)(ra.nextInt(26) + 97);
                    break;
            }
        }
        return text;
    }
}
